package com.yeban.test1;

import com.yeban.test1.entity.Student;
import com.yeban.test1.preparestatement.ManagerDao;
import com.yeban.test1.preparestatement.StudentDao1;

import java.sql.Date;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Scanner;

public class MainView {

    private static Scanner sc = new Scanner(System.in);
    private static ManagerDao managerDao = new ManagerDao();
    private static StudentDao1 dao = new StudentDao1();

    public static void main(String[] args) {

        if (!login()) {
            System.out.println("登录失败！程序退出");
            return;
        }

        while (true) {
            showMenu();
            String choice = sc.nextLine();
            switch (choice) {
                case "1":
                    countStudents();
                    break;
                case "2":
                    listStudents();
                    break;
                case "3":
                    queryById();
                    break;
                case "4":
                    queryByName();
                    break;
                case "5":
                    updateBirthday();
                    break;
                case "6":
                    deleteStudent();
                    break;
                case "0":
                    System.out.println("已退出系统");
                    return;
                default:
                    System.out.println("输入错误，请重试！");
            }
        }
    }

    // 登录
    public static boolean login() {
        System.out.print("请输入用户名：");
        String name = sc.nextLine();
        System.out.print("请输入密码：");
        String pwd = sc.nextLine();

        boolean ok = managerDao.login(name, pwd);
        if (ok) System.out.println("登录成功！");
        return ok;
    }

    // 菜单
    public static void showMenu() {
        System.out.println("\n=============== 请选择操作 ===============");
        System.out.println("1. 统计学生人数");
        System.out.println("2. 查看学生名单");
        System.out.println("3. 按学号查询学生");
        System.out.println("4. 按姓名查询学生");
        System.out.println("5. 修改学生生日");
        System.out.println("6. 删除学生记录");
        System.out.println("0. 退出");
        System.out.print("请输入操作键：");
    }

    // 1. 统计人数
    public static void countStudents() {
        System.out.println("===============统计操作===============");
        System.out.println("操作键：1");

        dao.selectNums();
        System.out.println();
    }

    // 2. 查看列表
    public static void listStudents() {
        System.out.println("===============查看操作===============");
        System.out.println("操作键：2");

        List<Student> stus = dao.selectAll();

        System.out.println("----------------------学生列表----------------------");
        System.out.println("学号\t姓名\t性别\t年级\t电话\t\t邮箱\t\t生日日期");

        for (Student stu : stus) {
            System.out.println(
                    stu.getId() + "\t" + stu.getName() + "\t" +
                            stu.getGender() + "\t" + stu.getGrade() + "\t" +
                            stu.getPhone() + "\t" + stu.getEmail() + "\t" +
                            stu.getBirthday());
        }
        System.out.println();
    }

    // 3. 按学号查询
    public static void queryById() {
        System.out.println("===============查询操作===============");
        System.out.println("操作键：3");

        System.out.print("请输入学生学号：");
        String id = sc.nextLine();

        Student stu = dao.selectOneById(id);

        if (stu != null) {
            System.out.println("查询成功！");
            System.out.println(id + " 号的学生姓名为：" + stu.getName() + "，所在年级：" + stu.getGrade());
        } else {
            System.out.println("学号不存在！");
        }
        System.out.println();
    }

    // 4. 按姓名查询
    public static void queryByName() {
        System.out.println("===============查询操作===============");
        System.out.println("操作键：4");

        System.out.print("请输入学生姓名（支持模糊）：");
        String name = sc.nextLine();

        List<Student> stus = dao.selectByName(name);

        System.out.println("----------------------学生列表----------------------");
        System.out.println("学号\t姓名\t性别\t年级\t电话\t\t邮箱\t\t生日日期");

        if (stus.isEmpty()) {
            System.out.println("未查询到结果！");
        } else {
            for (Student stu : stus) {
                System.out.println(
                        stu.getId() + "\t" + stu.getName() + "\t" +
                                stu.getGender() + "\t" + stu.getGrade() + "\t" +
                                stu.getPhone() + "\t" + stu.getEmail() + "\t" +
                                stu.getBirthday());
            }
        }
        System.out.println();
    }

    // 5. 修改生日
    public static void updateBirthday() {
        System.out.println("===============修改操作===============");
        System.out.println("操作键：5");

        System.out.print("请输入学生学号：");
        String id = sc.nextLine();

        System.out.print("请输入新的生日（yyyy-MM-dd）：");
        String dateStr = sc.nextLine();

        try {
            LocalDate localDate = LocalDate.parse(dateStr, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
            Date birthday = Date.valueOf(localDate);

            dao.updateBirthday(id, birthday);
            System.out.println("修改成功！");

        } catch (Exception e) {
            System.out.println("日期格式错误！请使用 yyyy-MM-dd 格式！");
        }
        System.out.println();
    }

    // 6. 删除学生
    public static void deleteStudent() {
        System.out.println("===============删除操作===============");
        System.out.println("操作键：6");

        System.out.print("请输入学生学号：");
        String id = sc.nextLine();
        Student stu = dao.selectOneById(id);
        if (stu == null) {
            System.out.println("学生不存在！");
            System.out.println();
            return;
        }
        System.out.println("确认要删除学号为 " + id + "，姓名为 " + stu.getName() + " 的学生信息吗？(Y/N)");
        String input = sc.nextLine().toUpperCase();

        if ("Y".equals(input)) {
            dao.deleteStu(id);
            System.out.println("删除成功！");
        } else {
            System.out.println("已取消删除！");
        }
        System.out.println();
    }
}
